package com.kerbores.utils.db.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.json.Json;
import org.nutz.lang.Files;
import org.nutz.lang.Lang;
import org.nutz.lang.Times;
import org.nutz.lang.random.R;

/**
 * @author Kerbores
 *
 *         Create At 2015年1月14日 上午10:21:45
 */
@Table("db_file")
public class DBFile {

	/**
	 * 编号
	 */
	@Id
	private long id;

	/**
	 * 文件名
	 */
	@Column("file_nema")
	private String name;

	/**
	 * 文件数据
	 */
	@Column("file_data")
	private byte[] data;

	/**
	 * 文件创建时间
	 */
	@Column("file_create_time")
	private Date createTime;

	/**
	 * 文件最后修改时间
	 */
	@Column("file_last_modify")
	private Date lastModify;
	/**
	 * 文件长度
	 */
	@Column("file_length")
	private long length;
	/**
	 * 文件所有者
	 */
	@Column("file_owner")
	private String owner;
	/**
	 * 文件类型
	 */
	@Column("file_type")
	private String type;
	/**
	 * 文件唯一标识
	 */
	@Column("file_uuid")
	private String uuid = R.UU16();
	/**
	 * 是否图片标识
	 */
	@Column("file_is_img")
	private boolean img;

	// =============== 非数据库持久字段 =============================
	/**
	 * 文件
	 */
	private File file;

	/**
	 * 
	 */
	public DBFile() {
		super();
	}

	/**
	 * @param file
	 */
	public DBFile(File file) {
		super();
		this.length = file.length();
		this.lastModify = Times.D(file.lastModified());
		this.createTime = Times.now();
		this.name = file.getName();
		this.data = getFileData(file);
		this.type = name.substring(name.lastIndexOf(".") + 1, name.length());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DBFile other = (DBFile) obj;
		if (id != other.id)
			return false;
		return true;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @return the data
	 */
	public byte[] getData() {
		return data;
	}

	/**
	 * @return the file
	 */
	public File getFile() {
		if (file != null) {
			return file;
		}
		String tmpDir = System.getProperty("java.io.tmpdir");
		File file = new File(tmpDir + File.separator + name);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(data);
			fos.flush();// 刷一下避免文件没写成功!!
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw Lang.makeThrow(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw Lang.makeThrow(e.getMessage());
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	/**
	 * @param file2
	 * @return
	 */
	private byte[] getFileData(File file) {
		return Files.readBytes(file);
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @return the lastModify
	 */
	public Date getLastModify() {
		return lastModify;
	}

	/**
	 * @return the length
	 */
	public long getLength() {
		return length;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the owner
	 */
	public String getOwner() {
		return owner;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @return the uuid
	 */
	public String getUuid() {
		return uuid;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	/**
	 * @return the img
	 */
	public boolean isImg() {
		return img;
	}

	/**
	 * @param createTime
	 *            the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @param data
	 *            the data to set
	 */
	public void setData(byte[] data) {
		this.data = data;
	}

	/**
	 * @param file
	 *            the file to set
	 */
	public void setFile(File file) {
		this.length = file.length();
		this.lastModify = Times.D(file.lastModified());
		this.createTime = Times.now();
		this.name = file.getName();
		this.data = getFileData(file);
		this.file = file;
		this.type = name.substring(name.lastIndexOf(".") + 1, name.length());
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @param img
	 *            the img to set
	 */
	public void setImg(boolean img) {
		this.img = img;
	}

	/**
	 * @param lastModify
	 *            the lastModify to set
	 */
	public void setLastModify(Date lastModify) {
		this.lastModify = lastModify;
	}

	/**
	 * @param length
	 *            the length to set
	 */
	public void setLength(long length) {
		this.length = length;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param owner
	 *            the owner to set
	 */
	public void setOwner(String owner) {
		this.owner = owner;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @param uuid
	 *            the uuid to set
	 */
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return Json.toJson(this);
	}

}
